const { createClient } = require("redis");
const dbConfig = require('../config/database');
module.exports = () => {

    const client = createClient({
        url: `redis://${dbConfig.redis.pass ? ':' + dbConfig.redis.pass : ''}@${dbConfig.redis.host}:${dbConfig.redis.port}/${dbConfig.redis.DbNumber}`
    });

    client.on('error', (err) => console.log('Redis Client Error', err));

    client.on('connect', (err) => {
        console.log('redis 连接成功!');
    });

    client.connect();

    global.redis = client;//绑定到nodejs 全局

    return async (ctx, next) => {
        ctx.redis = client;//绑定到 koa 上下文
        await next();
    }
}